function findByPid(url,data,callback){
	$.ajax({
		type : "GET",
		url : url,
		data :data,
		dataType : "json",
		success : function(data) {
			if(!showMsg(data)){
					return;
			}
			if(typeof(callback) == 'function'){
				callback(data.content);
			}
		}
	});
}

/**
 * 通过机构名称，编码查询机构
 */
function findByName(){
	var condition = {};
	condition.name = $("#organtreeform #oname").textbox('getValue');
	condition.code = $("#index #organCode").val();
	if($.trim(condition.name).length == 0){
		message("机构名称不能为空");
		return;
	}
	$.ajax({
		type : "GET",
		url : ctx + "/tree/query",
		data :condition,
		dataType : "json",
		success : function(data) {
			if(!showMsg(data)){
					return;
			}
			if(data.content.length == 0){
				message("未查询到机构");
				return;
			}
			initOrganTree(data.content);
			getTreeObj().expandAll(true);
		}
	});
}

var demoMsg = {
	async : "正在进行异步加载，请等一会儿再点击...",
	expandAllOver : "全部展开完毕",
	asyncAllOver : "后台异步加载完毕",
	asyncAll : "已经异步加载完毕，不再重新加载",
	expandAll : "已经异步加载完毕，使用 expandAll 方法"
}

/**
 * 
 * @param treeId
 * @param parentNode
 * @param childNodes
 * @returns
 */
function filter(treeId, parentNode, data) {
	if(data.result == 'fail'){
		message(data.errorCode + ":" + data.errorMessage);
		return;
	}
	return data.content;
}

function beforeAsync() {
	curAsyncCount++;
}

function onAsyncSuccess(event, treeId, treeNode, msg) {
	curAsyncCount--;
	if (curStatus == "expand") {
		expandNodes(treeNode.children);
	} else if (curStatus == "async") {
		asyncNodes(treeNode.children);
	}

	if (curAsyncCount <= 0) {
		if (curStatus != "init" && curStatus != "") {
			$("#demoMsg").text(
					(curStatus == "expand") ? demoMsg.expandAllOver
							: demoMsg.asyncAllOver);
			asyncForAll = true;
		}
		curStatus = "";
	}
	
	if(typeof(onAsyncSuccessAfterCallBack) == 'function'){
		onAsyncSuccessAfterCallBack(treeNode.children);
	}
}

function onAsyncError(event, treeId, treeNode, XMLHttpRequest, textStatus,
		errorThrown) {
	curAsyncCount--;

	if (curAsyncCount <= 0) {
		curStatus = "";
		if (treeNode != null)
			asyncForAll = true;
	}
}

function expandAll() {
	if (!check()) {
		return;
	}
	var zTree = getTreeObj();
	if (asyncForAll) {
		$("#demoMsg").text(demoMsg.expandAll);
		zTree.expandAll(true);
	} else {
		expandNodes(zTree.getNodes());
		if (!goAsync) {
			$("#demoMsg").text(demoMsg.expandAll);
			curStatus = "";
		}
	}
}
function expandNodes(nodes) {
	if (!nodes)
		return;
	curStatus = "expand";
	var zTree = getTreeObj();
	for (var i = 0, l = nodes.length; i < l; i++) {
		zTree.expandNode(nodes[i], true, false, false);
		if (nodes[i].isParent && nodes[i].zAsync) {
			expandNodes(nodes[i].children);
		} else {
			goAsync = true;
		}
	}
}

function asyncAll() {
	if (!check()) {
		return;
	}
	var zTree = getTreeObj();
	if (asyncForAll) {
		$("#demoMsg").text(demoMsg.asyncAll);
	} else {
		asyncNodes(zTree.getNodes());
		if (!goAsync) {
			$("#demoMsg").text(demoMsg.asyncAll);
			curStatus = "";
		}
	}
}
function asyncNodes(nodes) {
	if (!nodes)
		return;
	curStatus = "async";
	var zTree = getTreeObj();
	for (var i = 0, l = nodes.length; i < l; i++) {
		if (nodes[i].isParent && nodes[i].zAsync) {
			asyncNodes(nodes[i].children);
		} else {
			goAsync = true;
			zTree.reAsyncChildNodes(nodes[i], "refresh", true);
		}
	}
}

function check() {
	if (curAsyncCount > 0) {
		$("#demoMsg").text(demoMsg.async);
		return false;
	}
	return true;
}


/**
 * 重置机构树
 */
function reset() {
	if (!check()) {
		return;
	}
	asyncForAll = false;
	goAsync = false;
	$("#demoMsg").text("");
	getTree();
}